package cn.szzsi.platform.controller.admin;

import cn.szzsi.AConfig;
import cn.szzsi.common.util.CommonUtil;
import cn.szzsi.model.Member;
import cn.szzsi.model.mall.MemberService;
import cn.szzsi.platform.service.AgentService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.List;

public class AgentCertificationController extends Controller{
    public void list(){
        render(CommonUtil.TEMPLATE_PATH + "/platform/agent/agent_certification_list.html");
    }
    public void ajaxlist(){
        String agentStates=getPara("agentStates");
        String sql="select * from sys_member where( type=0 or type=2) and agentStates="+agentStates+" order by agentApplyDate desc";
        List<Member>memberList=Member.dao.find(sql);
        renderJson(memberList);
    }

    public void detail(){
        String id=getPara("id");
        Member member=Member.dao.findById(id);
        setAttr("member",member);
        String parentId=member.getAgentPid();
        if(StrKit.notBlank(parentId)){
            Member pidMember=Member.dao.findById(parentId);
            setAttr("pidMember",pidMember);
        }
        render(CommonUtil.TEMPLATE_PATH+"/platform/agent/agent_certification_detail.html");
    }

    /**
     * 审核通过
     */
    @Before(Tx.class)
    public void hasverified(){
        String id=getPara("id");
        String remark=getPara("remark");
        Integer agentLevel=getParaToInt("agentLevel",0);
        final Member member=Member.dao.findById(id);
        if(2==member.getAgentStates()){
            renderJson(Ret.fail("info","该代理商已经审核通过!"));
            return;
        }
        member.setAgentStates(2);//审核通过
        member.setAgentLevel(agentLevel);
        member.setRemark(remark);
        member.update();
        AConfig.fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                MemberService.service.updateMember(member.getPhone());//将代理商添加平台会员
                AgentService.service.recommendMoneyToAgent(member);
            }
        });
        renderJson(Ret.ok());
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }
}
